class LuckySheet {

    /**
     * 初始化
     * @param dom  dom节点
     * @param options 配置项
     */
    constructor(dom, options) {
        this.options = options;
        luckysheet.create(options);

        window['getValue'] = this.getData;
        window['setValue'] = this.setData;
    }

    /**
     * 获取单元格数据
     * @param cell
     */
    getData(cell) {
        if (cell == undefined || cell.trim() == "") {
            return [[]];
        }
        var data = luckysheet.getRangeValue({range: cell});
        // 数据处理
        // 二维数组处理
        var newData = [];
        for (var item of data) {
            var k = [];
            for (var el of item) {
                if (el == null) {
                    continue
                } else {
                    k.push(el.v);
                }
            }
            newData.push(k)
        }
        return newData;
    }

    /**
     * 设置单元格数据
     * @param c
     * @param data
     */
    setData(c, v) {
        // 判断数组类型
        var mbCell = luckysheet.getRangeArray("twoDimensional", {range: c});
        // 目标单元格的大小
        // 填充到指定大小
        var data = [];
        if (typeof (v) == 'string') {
            for (var item of mbCell) {
                var h = [];
                for (var el of item) {
                    h.push({
                        "m": v,
                        "ct": {
                            "fa": "General",
                            "t": "g"
                        },
                        "v": v
                    });
                }
                data.push(h);
            }
            luckysheet.setRangeValue(data, c);
        } else {
            var type = "arr"
            var arr2Length = 0;
            for (var item of v) {
                if (Array.isArray(item)) {
                    type = "arr2";
                    arr2Length = item.length;
                    break;
                }
            }

            if (type == "arr") {
                var begin = 0;
                var length = v.length;
                for (var item of mbCell) {
                    var h = [];
                    for (var el of item) {
                        h.push({
                            "m": v[begin],
                            "ct": {
                                "fa": "General",
                                "t": "g"
                            },
                            "v": v[begin]
                        });
                        if (begin > length) {
                            begin = 0;
                        } else {
                            begin++;
                        }
                    }
                    data.push(h);
                }
            } else {
                var begin = 0;
                var allData = v.reduce(function (a, b) {
                    return a.concat(b)
                });
                var length = allData.length;
                for (var item of mbCell) {
                    var h = [];
                    for (var el of item) {
                        h.push({
                            "m": allData[begin],
                            "ct": {
                                "fa": "General",
                                "t": "g"
                            },
                            "v": allData[begin]
                        });
                        if (begin > length) {
                            begin = 0;
                        } else {
                            begin++;
                        }
                    }
                    data.push(h);
                }
            }
        }
        luckysheet.setRangeValue(data, {range: c});
        luckysheet.refresh();
    }

    /**
     * 选中选区
     * @param cell
     */
    choseCell(cell) {
        luckysheet.setRangeShow(cell);
    }

    /**
     * 画布保存到本地
     */
    saveLuckySheetLocal() {
        var luckySheet = {
            time: new Date(),
            data: this.canvas
        }
        window.localStorage.setItem('luckySheet', JSON.stringify(this.options));
    }
}

export {LuckySheet}